package chapter17.chapter17_12;//: containers/References.java
// Demonstrates Reference objects

import java.lang.ref.*;
import java.util.*;

class VeryBig {
    private static final int SIZE = 10000;
    private long[] la = new long[SIZE];
    private String ident;

    public VeryBig(String id) {
        ident = id;
    }

    public String toString() {
        return ident;
    }

    protected void finalize() {
        System.out.println("Finalizing " + ident);
    }
}

public class References {
    private static ReferenceQueue<VeryBig> rq =
            new ReferenceQueue<VeryBig>();

    public static void checkQueue() {
        Reference<? extends VeryBig> inq = rq.poll();
        if (inq != null)
            System.out.println("In queue: " + inq.get());
    }

    public static void main(String[] args) {
        int size = 10;
        // Or, choose size via the command line:
        if (args.length > 0)
            size = new Integer(args[0]);
        LinkedList<SoftReference<VeryBig>> sa =
                new LinkedList<SoftReference<VeryBig>>();
        for (int i = 0; i < size; i++) {
            sa.add(new SoftReference<VeryBig>(
                    new VeryBig("Soft " + i), rq));
            System.out.println("Just created: " + sa.getLast());
            checkQueue();
        }
        LinkedList<WeakReference<VeryBig>> wa =
                new LinkedList<WeakReference<VeryBig>>();
        for (int i = 0; i < size; i++) {
            wa.add(new WeakReference<VeryBig>(
                    new VeryBig("Weak " + i), rq));
            System.out.println("Just created: " + wa.getLast());
            checkQueue();
        }
        SoftReference<VeryBig> s =
                new SoftReference<VeryBig>(new VeryBig("Soft"));
        WeakReference<VeryBig> w =
                new WeakReference<VeryBig>(new VeryBig("Weak"));
        System.gc();
        LinkedList<PhantomReference<VeryBig>> pa =
                new LinkedList<PhantomReference<VeryBig>>();
        for (int i = 0; i < size; i++) {
            pa.add(new PhantomReference<VeryBig>(
                    new VeryBig("Phantom " + i), rq));
            System.out.println("Just created: " + pa.getLast());
            checkQueue();
        }
    }
} /*
 Just created: java.lang.ref.SoftReference@677327b6
Just created: java.lang.ref.SoftReference@14ae5a5
Just created: java.lang.ref.SoftReference@7f31245a
Just created: java.lang.ref.SoftReference@6d6f6e28
Just created: java.lang.ref.SoftReference@135fbaa4
Just created: java.lang.ref.SoftReference@45ee12a7
Just created: java.lang.ref.SoftReference@330bedb4
Just created: java.lang.ref.SoftReference@2503dbd3
Just created: java.lang.ref.SoftReference@4b67cf4d
Just created: java.lang.ref.SoftReference@7ea987ac
Just created: java.lang.ref.WeakReference@12a3a380
Just created: java.lang.ref.WeakReference@29453f44
Just created: java.lang.ref.WeakReference@5cad8086
Just created: java.lang.ref.WeakReference@6e0be858
Just created: java.lang.ref.WeakReference@61bbe9ba
Just created: java.lang.ref.WeakReference@610455d6
Just created: java.lang.ref.WeakReference@511d50c0
Just created: java.lang.ref.WeakReference@60e53b93
Just created: java.lang.ref.WeakReference@5e2de80c
Just created: java.lang.ref.WeakReference@1d44bcfa
Finalizing Weak
Just created: java.lang.ref.PhantomReference@266474c2
Finalizing Weak 9
Finalizing Weak 8
In queue: null
Finalizing Weak 7
Just created: java.lang.ref.PhantomReference@6f94fa3e
Finalizing Weak 6
Finalizing Weak 5
Finalizing Weak 4
Finalizing Weak 3
Finalizing Weak 2
Finalizing Weak 1
Finalizing Weak 0
In queue: null
Just created: java.lang.ref.PhantomReference@5e481248
In queue: null
Just created: java.lang.ref.PhantomReference@66d3c617
In queue: null
Just created: java.lang.ref.PhantomReference@63947c6b
In queue: null
Just created: java.lang.ref.PhantomReference@2b193f2d
In queue: null
Just created: java.lang.ref.PhantomReference@355da254
In queue: null
Just created: java.lang.ref.PhantomReference@4dc63996
In queue: null
Just created: java.lang.ref.PhantomReference@d716361
In queue: null
Just created: java.lang.ref.PhantomReference@6ff3c5b5
In queue: null*///:~
